package com.itheima.servlet.response;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

// 演示重定向
@WebServlet("/resp01")
public class ResponseDemo01 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("resp01....");
        // 1.设置响应状态码 201
        // response.setStatus(201);

        // 2.设置响应头 Content-Type: text/html
        // response.setHeader("Content-Type", "text/html");


        // 重定向
        // 1.设置响应状态码 302
        // response.setStatus(302);
        // 2.设置响应头 Location
        // response.setHeader("Location", "resp02");

        // 简化方式完成重定向, 重定向是,资源路径可以直接写:"resp02"   或者  request.getContextPath() + "/resp02"
        // 动态获取虚拟目录
        response.sendRedirect(request.getContextPath() + "/resp02");
    }
}
